/**
 * 
 */
package cn.edu.nju.ws.sview.wyge.relations.adataset.relations.datasetRelations;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashSet;

import cn.edu.nju.ws.sview.cache.URIIDCache;
import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.wyge.relations.adataset.CustomDataset;

/**
 * @author Weiyi Ge (geweiyi@gmail.com) 2012-8-12
 * 
 */
public class DatasetConstants {
	public static HashSet<String> get(String ds_type, int ds_id) {
		ArrayList<Integer> docs = new ArrayList<Integer>();
		if (ds_type.equals("custom")) {
			docs = CustomDataset.getURLIDsByCustomDatasetId(ds_id);
		} else if (ds_type.equals("uri")) {
			docs = new ArrayList<Integer>();
			docs.add(ds_id);
		}
		return get(docs);
	}

	public static HashSet<String> get(ArrayList<Integer> docs) {
		HashSet<String> res = new HashSet<String>();
		Connection conn = DBConnectionFactory.getConnection();//.getFalconConnection();
		try {
			PreparedStatement selps = conn
					.prepareStatement("SELECT s,p,o FROM quadruple where doc_id = ?");//("SELECT s,p,o FROM quadruple where c = ?");
			int typeid = 0;
			try {
				typeid = URIIDCache.getInstance().getURIID(
						"http://www.w3.org/1999/02/22-rdf-syntax-ns#type", false);
			} catch (Throwable e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			for (int doc : docs) {
				selps.setInt(1, doc);
				ResultSet selrs = selps.executeQuery();
				while (selrs.next()) {
					String s = selrs.getString(1);
					String p = selrs.getString(2);
					int pid = Integer.parseInt(p.substring(1));
					String o = selrs.getString(3);
					res.add("1" + s);
					if (pid != typeid) {
						res.add("2" + pid);
					}
					res.add("3" + o);
				}
				selrs.close();
			}
			selps.close();
			conn.close();
		} catch (Throwable t) {
			t.printStackTrace();
		}
		return res;
	}
}
